package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.ResultInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.util.Utils;

import java.text.MessageFormat;
import java.util.List;

public class TomorrowEdgeAnalyzer extends AbstractStockAnalyzer {

    int daysToNow = 12;
    double maxTurnOver = 10;
    double minTurnOver = 3;

    double closeRangeTop = 0.15;
    double closeRangeDown = 0;

    public TomorrowEdgeAnalyzer() {
    }

    @Override
    public boolean analyze(ResultInfo resultInfo, Stock stock) {
        boolean ok = false;

        List<KLineInfo> infos = stock.getkLineInfos();
        if (infos.size() < daysToNow) {
            // 元数据天数要求大于考察天数
            return ok;
        }

        double max = 0;
        double min = 9999;
        double totalTurnover = 0;
        int redBar = 0;
        int greenBar = 0;

        for (int i = infos.size() - daysToNow; i < infos.size(); i++) {
            KLineInfo line = infos.get(i);

            max = Math.max(max, line.getClose());
            min = Math.min(min, line.getClose());
            totalTurnover += line.getTurnoverRate();
            if (line.getClose() >= line.getOpen()) {
                redBar++;
            } else {
                greenBar++;
            }
        }

        boolean condition1 = redBar > greenBar;

        boolean condition2 = (totalTurnover < maxTurnOver * daysToNow) && (totalTurnover > minTurnOver * daysToNow);

        boolean condition3 = ((max / min - 1) < closeRangeTop) && ((max / min - 1) > closeRangeDown);

        if (condition1 && condition2 && condition3) {
            String msg = format(stock, min, max);
            ok = true;
            resultInfo.appendMessage(msg);
        }
        resultInfo.setValue(getTurnover(stock));
        return ok;
    }

    public String format(Stock stock, double min, double max) {
        StringBuilder sb = new StringBuilder();
        String label = Utils.calcRangeLabel(min, max);
        sb.append("波动：").append(label)
                .append("\n");
        sb.append("现价: ").append(getCurrentPrice(stock));
        return (sb.toString());
    }

    @Override
    public String getDescription() {
        String format = "在最近{0}天内，平均换手率大于{1}% 而且 小于{2}%, 红多绿少，总体波动大于{3}%并且小于{4}%";
        return MessageFormat.format(format, daysToNow, minTurnOver, maxTurnOver, closeRangeDown * 100, closeRangeTop * 100);
    }

    public int getDaysToNow() {
        return daysToNow;
    }

    public void setDaysToNow(int daysToNow) {
        this.daysToNow = daysToNow;
    }

    public double getCloseRangeTop() {
        return closeRangeTop;
    }

    public void setCloseRangeTop(double closeRangeTop) {
        this.closeRangeTop = closeRangeTop;
    }

    public double getCloseRangeDown() {
        return closeRangeDown;
    }

    public void setCloseRangeDown(double closeRangeDown) {

        this.closeRangeDown = closeRangeDown;
    }
    public int getWeight() {
        return 50;
    }
}
